//
//  MainViewController.m
//  JXW_UnionSDK
//
//  Created by 彭月辉 on 2022/6/23.
//

#import "MainViewController.h"
#import <ADSEFramework/ADSELinkManager.h>

#import "Masonry.h"

#import "PreviewViewController.h"
#import "LocalFileViewController.h"

#define APPKEY     @"c2166bbbc1861c8b309204f59c276e188599595a57d83cc22d1703cb370d2a1a"
#define APPSECRET  @"1ed82be8de78600f7dedd539adccb1e6dec8b29650073198e383aad7a2d096d8"


@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initUI];
}

- (void)connectDevice{

    [SVProgressHUD show];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //连接设备
        [[ADSELinkManager shareInstance] connect:^(NSInteger code) {
            NSLog(@"连接设备 --> %@",code == 0 ? @"成功":@"失败");
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                if(code == LINK_ERR_OK){
                    PreviewViewController *previewVC = [[PreviewViewController alloc] init];
                    [self.navigationController pushViewController:previewVC animated:YES];
                }
                else{
                    //连接设备失败
                    [self showError:@"设备连接失败"];
                }
            });
        }];
        
    });
    
    
}

- (void)checkLocalFile{
    
    LocalFileViewController *localfileVC = [[LocalFileViewController alloc] init];
    [self.navigationController pushViewController:localfileVC animated:YES];

}

- (void)initUI{
    UIButton *connectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:connectBtn];
    
    [connectBtn setTitle:@"连接设备" forState:UIControlStateNormal];
    [connectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [connectBtn setBackgroundColor:[UIColor blueColor]];
    [connectBtn addTarget:self action:@selector(connectDevice) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *localfileBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:localfileBtn];
    
    [localfileBtn setTitle:@"本地文件" forState:UIControlStateNormal];
    [localfileBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [localfileBtn setBackgroundColor:[UIColor blueColor]];
    [localfileBtn addTarget:self action:@selector(checkLocalFile) forControlEvents:UIControlEventTouchUpInside];


    [connectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_centerY).offset(-50);
        make.width.equalTo(@200);
        make.height.equalTo(@50);
    }];
    
    [localfileBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view.mas_centerY).offset(50);
        make.width.equalTo(@200);
        make.height.equalTo(@50);
    }];
}

@end
